package color.koitq.picker.util;

import android.app.Activity;

import androidx.fragment.app.Fragment;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.util.List;

public class MyPermissionsUtils {

    public static void requestPermissionsTurn(
            Activity activity, HavePermissionListener listener, String... permissions) {
        XXPermissions.with(activity).permission(permissions).request(new OnPermissionCallback() {
            boolean isShowDialog = false;
            @Override
            public void onGranted(List<String> permissions, boolean all) {
                if (all) {
                    if (listener != null) {
                        listener.havePermission();
                    }
                } else {
                    if (!isShowDialog){
                        turnSystemPermission(activity, permissions);
                        isShowDialog = true;
                    }
                }
            }

            @Override
            public void onDenied(List<String> permissions, boolean never) {
                if (!isShowDialog){
                    turnSystemPermission(activity, permissions);
                    isShowDialog = true;
                }
            }
        });
    }

    public static void requestPermissionsTurn(
            Fragment fragment, HavePermissionListener listener, String... permissions) {
        XXPermissions.with(fragment).permission(permissions).request(new OnPermissionCallback() {
            boolean isShowDialog = false;
            @Override
            public void onGranted(List<String> permissions, boolean all) {
                if (all) {
                    if (listener != null) {
                        listener.havePermission();
                    }
                } else {
                    if (!isShowDialog){
                        turnSystemPermission(fragment, permissions);
                        isShowDialog = true;
                    }
                }
            }

            @Override
            public void onDenied(List<String> permissions, boolean never) {
                if (!isShowDialog){
                    turnSystemPermission(fragment, permissions);
                    isShowDialog = true;
                }
            }
        });
    }

    private static void turnSystemPermission(Activity activity, List<String> permissions) {
        new QMUIDialog.MessageDialogBuilder(activity)
                .setMessage("未授予相关权限，功能无法正常使用，是否去授权？")
                .addAction("否", (dialog, index) -> dialog.dismiss())
                .addAction("是", (dialog, index) -> {
                    dialog.dismiss();
                    XXPermissions.startPermissionActivity(activity, permissions);
                }).show();
    }

    private static void turnSystemPermission(Fragment fragment, List<String> permissions) {
        new QMUIDialog.MessageDialogBuilder(fragment.getContext())
                .setMessage("未授予相关权限，功能无法正常使用，是否去授权？")
                .addAction("否", (dialog, index) -> dialog.dismiss())
                .addAction("是", (dialog, index) -> {
                    dialog.dismiss();
                    XXPermissions.startPermissionActivity(fragment, permissions);
                }).show();
    }

    public interface HavePermissionListener {

        void havePermission();
    }
}
